
class myPromise{
  constructor(Fn){
     this.successEventQueue = [];
     this.failEventQueue = [];
     this.state = "pending";
     setTimeout(()=>{
        Fn(this.reslove.bind(this),this.reject.bind(this))
     },0)
 
  }

  then(sucessFn){
    if( typeof sucessFn != "function") throw new Error('then的参数必须为一个函数');
   // if(this.state != "pending") throw new Error("then和catch不能同时执行")
    this.successEventQueue.push(sucessFn);
    return this
  }
  
  catch(failFn){
    if( typeof failFn != "function") throw new Error('catch的参数必须为一个函数');
   // if(this.state != "pending") throw new Error("then和catch不能同时执行")
    this.failEventQueue.push(failFn);
    return this 
  }

  reslove(res){
      if(this.state != "pending") throw new Error("then和catch不能同时执行")
      this.state = "fullfilled";
      let result = res;
      this.successEventQueue.forEach((item,index)=>{
        result = item(result)
      })
  }

  reject(res){
   
     this.state = 'rejected';
     this.failEventQueue.forEach((item,index)=>{
        item(res)
    })
  } 
}

new myPromise(function(reslove,reject){
    reslove("111")
    reject("222")
}).then(res=>{
    console.log(res)
}).catch()

